package com.plm.maque.common.global;


import com.plm.maque.common.exception.BizException;
import com.plm.maque.common.exception.CodeEnum;
import com.plm.maque.domain.vo.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Limi
 * @create 2020-06-27 13:33
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义的业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public ResultBody bizExceptionHandler(BizException e){
        log.error("业务异常: {}", e.getMsg());
        return ResultBody.error(e.getCode(),e.getMsg());
    }

    /**
     * 处理空指针的异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public ResultBody exceptionHandler(NullPointerException e){
        log.error("空指针异常: ", e);
        return ResultBody.error(CodeEnum.BODY_NOT_MATCH);
    }

    /**
     * 处理其他异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultBody exceptionHandler(Exception e){
        log.error("系统异常: ", e);
        return ResultBody.error(CodeEnum.INTERNAL_SERVER_ERROR);
    }

}
